package com.ringtheweb.web.lifecycle;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;

import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpMessage;

@Name("smtpService")
@Scope(ScopeType.APPLICATION)
public class SmtpService {
	
	@Out SimpleSmtpServer smtpServer;
	
	@Out(scope=ScopeType.EVENT,required=false)
	List<SmtpMessage> emails ;
	
	@Observer("org.jboss.seam.postInitialization")
	public void start() {
		smtpServer = SimpleSmtpServer.start(2525);
	}
	

	
    @Factory("emails")
    public synchronized void listMessages() {
    	emails = new ArrayList<SmtpMessage>();
    	for (Iterator it = smtpServer.getReceivedEmail();it.hasNext();) {
    		 SmtpMessage email = (SmtpMessage)it.next();
    		 emails.add(email);
    	}
    	
    }
	
}
